package com.tau.egonetwork.gui.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.gui.Environment;

public class EgoNetworkProgressDialog extends Dialog {

	private String _title;
	private String _subtitle;
	private String _message;
	private int _progress;
	
	private Label headerLabel;
	private Label subHeaderLabel;
	private Label textLabel;
	private ProgressBar progressBar;
	
	private boolean displayCloseButton = true;
	
	private Shell shell;
	
	public EgoNetworkProgressDialog(Shell parent) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	}
	
	public void setTitle(String title) {
		_title = title;
		if (shell != null) {
			if (!shell.isDisposed() && shell.isVisible()) {
				headerLabel.setText(_title);
				headerLabel.redraw();
			}			
		}
	}
	
	public void setSubtitle(String subtitle) {
		_subtitle = subtitle;
		if (shell != null) {
			if (!shell.isDisposed() && shell.isVisible()) {
				subHeaderLabel.setText(_subtitle);
				subHeaderLabel.redraw();
			}			
		}
	}
	
	public void setMessage(String message) {
		_message = message;
		if (shell != null) {
			if (!shell.isDisposed() && shell.isVisible()) {
				textLabel.setText(_message);
				textLabel.redraw();
			}			
		}
	}
	
	public void setProgress(int progress) {
		_progress = progress;
		if (shell != null) {
			if (!shell.isDisposed() && shell.isVisible()) {
				progressBar.setSelection(progress);
				progressBar.redraw();
			}			
		}
	}
	
	public boolean isActive() {
		if (shell == null || shell.isDisposed())
			return false;
		if (progressBar.getSelection() == 100)
			return false;
		return true;
	}
	
	public void setVisible(boolean visible) {
		shell.setVisible(visible);
	}
	
	public boolean getVisible() {
		return shell.getVisible();
	}
	
	public void close() {
		shell.close();
	}
	
	public void open() {
		open(true);
	}
	
	public void open(boolean displayCloseButton) {
		shell = new Shell(getParent(), getStyle());
		shell.setText(getText());
		
		this.displayCloseButton = displayCloseButton;
		createDialogContent(shell);
		
		shell.pack();
		
		//set shell at the center of display:
	    Monitor primary = getParent().getMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		
		shell.open();

	}
	
	private void createDialogContent(final Shell shell) {
		
		GridLayout dialogLayout = new GridLayout(1, true);
		dialogLayout.marginWidth = 0;
		dialogLayout.marginHeight = 0;
		dialogLayout.verticalSpacing = 0;
		shell.setLayout(dialogLayout);
		shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Composite headerComposite = new Composite(shell, SWT.NONE);
		headerComposite.setBackgroundImage(Environment.BG_DIALOG_TITLE);
		headerComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		GridLayout headerCompositeLayout = new GridLayout(1, false);
		headerCompositeLayout.marginTop = 5;
		headerCompositeLayout.marginBottom = 10;
		headerCompositeLayout.marginLeft = 3;
		headerCompositeLayout.marginRight = 3;
		headerCompositeLayout.verticalSpacing = 10;
		headerComposite.setLayout(headerCompositeLayout);
		GridData headerCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		headerCompositeGridData.widthHint = 550;
		headerComposite.setLayoutData(headerCompositeGridData);
		
		headerLabel = new Label(headerComposite, SWT.NONE);
		headerLabel.setText(_title);
		headerLabel.setFont(new Font(null, "Ariel", 11, SWT.BOLD));
		
		subHeaderLabel = new Label(headerComposite, SWT.NONE);
		subHeaderLabel.setText(_subtitle);
		
		Label separtator = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		separtator.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		
		Composite progressComposite = new Composite(shell, SWT.NONE);
		GridLayout progressCompositeLayout = new GridLayout(1, true);
		progressCompositeLayout.marginTop = 5;
		progressCompositeLayout.marginBottom = 5;
		progressCompositeLayout.marginLeft = 4;
		progressCompositeLayout.marginRight = 4;
		progressCompositeLayout.verticalSpacing = 10;
		progressComposite.setLayout(progressCompositeLayout);
		progressComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		textLabel = new Label(progressComposite, SWT.NONE);
		textLabel.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, true, false));
		textLabel.setText(_message);
		
		progressBar = new ProgressBar(progressComposite, SWT.SMOOTH | SWT.HORIZONTAL);
		progressBar.setMinimum(0);
		progressBar.setMaximum(100);
		progressBar.setSelection(_progress);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Composite closeComposite = new Composite(shell, SWT.NONE);
		closeComposite.setLayout(new GridLayout(1, false));
		closeComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		
		if (displayCloseButton) {
			
			final Button closeButton = new Button(closeComposite, SWT.PUSH);
			closeButton.setImage(Environment.ICON_CLOSE);
			closeButton.setText("Close");
			
			closeButton.addListener(SWT.Selection, new Listener() {
				
				@Override
				public void handleEvent(Event event) {
					if (_progress == 100) {
						shell.close();
					} else {
						shell.setVisible(false);
					}
				}
			});
		}
		
		shell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {
				if (_progress == 100) {
					//dispose shell:
					return; //let super handle this
				}
				//otherwise, take care of event yourself:
				event.doit = false;
				shell.setVisible(!displayCloseButton);
			}
			
		});
	}
}
